<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/21/24
 * Time: 11:46 AM
 */

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use App\Services\KfAccountService;
use App\Http\Controllers\ResourceController;

class KfAccountController extends ResourceController
{
    protected $service;

    public function __construct(KfAccountService $service)
    {
        $this->service = $service;
    }


    /**
     * 数据/消息回调
     *
     * @param Request $request
     * @return mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function dataCallback(Request $request)
    {
        $msgSignature = $request->get('msg_signature');
        $timestamp    = $request->get('timestamp');
        $nonce        = $request->get('nonce');
        $echostr      = $request->get('echostr');
        $contents     = $request->getContent();

        $returnStr = '';

        $errCode = $this->service->dataCallback($msgSignature, $timestamp, $nonce, $echostr, $contents, $returnStr);

        return $errCode == 0 ? $returnStr : $this->responseWithError("ERR: " . $errCode);
    }


    /**
     * 上传图片
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function uploadImage(Request $request)
    {
        $file   = $request->file('file');
        $corpId = $request->post('corp_id', '');
        $result = $this->service->uploadImg($corpId, $file);
        if (!$result) {

            return $this->responseWithError('上传失败');
        }

        return $this->responseWithSuccess($result);
    }


    /**
     * 上传临时素材
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function uploadMedia(Request $request)
    {
        $file   = $request->file('file');
        $corpId = $request->post('corp_id', '');
        $result = $this->service->uploadMedia($corpId, $file);
        if (!$result) {

            return $this->responseWithError('上传失败');
        }

        return $this->responseWithSuccess($result);
    }


    /**
     * 获取临时素材
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getMedia(Request $request)
    {
        $corpId  = $request->post('corp_id');
        $mediaId = $request->post('media_id');

        return $this->responseWithSuccess([
            'url' => $this->service->getMedia($corpId, $mediaId)
        ]);
    }


    /**
     * 添加客服账号
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function addKfAccount(Request $request)
    {
        $params = $request->post();

        return $this->service->addKfAccount($params) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取企业微信客服列表
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getKfAccountList(Request $request)
    {
        $corpId = $request->post('corp_id', '');

        return $this->responseWithSuccess($this->service->getKfAccountList($corpId));
    }


    /**
     * 删除客服账号
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function delKfAccount(Request $request)
    {
        $corpId   = $request->post('corp_id');
        $openKfid = $request->post('open_kfid');

        return $this->service->delKfAccount($corpId, $openKfid) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服链接
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getKfLink(Request $request)
    {
        $corpId   = $request->post('corp_id');
        $openKfid = $request->post('open_kfid');

        $result = $this->service->getKfLink($corpId, $openKfid);

        return $result ? $this->responseWithSuccess($result) : $this->responseWithError();
    }



    /**
     * 获取客户列表
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getKfCustomerList(Request $request)
    {
        $params = $request->post();

        return $this->responseWithSuccess($this->service->getKfCustomerList($params));
    }


    /**
     * 设置客服默认回复
     *
     * @param Request $request
     * @return mixed
     */
    public function setKfDefault(Request $request)
    {
        return $this->service->setKfDefault($request->post()) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服默认回复
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfDefault(Request $request)
    {
        return $this->responseWithSuccess($this->service->getKfDefault($request->post('open_kfid')));
    }


    /**
     * 设置客服欢迎语
     *
     * @param Request $request
     * @return mixed
     */
    public function setKfWelcome(Request $request)
    {
        return $this->service->setKfWelcome($request->post()) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服欢迎语
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfWelcome(Request $request)
    {
        return $this->responseWithSuccess($this->service->getKfWelcome($request->post('open_kfid')));
    }


    /**
     * 获取客服消息类型
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfMsgType(Request $request)
    {
        return $this->responseWithSuccess($this->service->getKfMsgType());
    }


    /**
     * 设置客服关键字
     *
     * @param Request $request
     * @return mixed
     */
    public function setKfKeyword(Request $request)
    {
        return $this->service->setKfKeyword($request->post()) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服欢关键字
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfKeyword(Request $request)
    {
        return $this->responseWithSuccess(
            $this->service->getKfKeyword($request->post('open_kfid'))
        );
    }


    /**
     * 删除客服关键字
     *
     * @param Request $request
     * @return mixed
     */
    public function delKfKeyword(Request $request)
    {
        return $this->service->delKfKeyword($request->post('id')) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取推送间隔时间
     *
     * @return mixed
     */
    public function getPushTime()
    {
        return $this->responseWithSuccess($this->service->getPushTime());
    }


    /**
     * 设置客服推送
     *
     * @param Request $request
     * @return mixed
     */
    public function setKfPush(Request $request)
    {
        return $this->service->setKfPush($request->post()) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服推送
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfPush(Request $request)
    {
        $corpId   = $request->post('corp_id', '');
        $openKfid = $request->post('open_kfid', '');

        return $this->responseWithSuccess($this->service->getKfPush($corpId, $openKfid));
    }


    /**
     * 删除客服推送
     *
     * @param Request $request
     * @return mixed
     */
    public function delKfPush(Request $request)
    {
        return $this->service->delKfPush($request->post('id', 0)) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 设置客服定时推送
     *
     * @param Request $request
     * @return mixed
     */
    public function setKfTimingPush(Request $request)
    {
        return $this->service->setKfTimingPush($request->post()) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }


    /**
     * 获取客服定时推送
     *
     * @param Request $request
     * @return mixed
     */
    public function getKfTimingPush(Request $request)
    {
        $corpId   = $request->post('corp_id', '');
        $openKfid = $request->post('open_kfid', '');

        return $this->responseWithSuccess($this->service->getKfTimingPush($corpId, $openKfid));
    }


    /**
     * 删除客服定时推送
     *
     * @param Request $request
     * @return mixed
     */
    public function delKfTimingPush(Request $request)
    {
        return $this->service->delKfTimingPush($request->post('id', 0)) ?
            $this->responseWithSuccess() : $this->responseWithError();
    }
}
